<!DOCTYPE HTML>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="stylesheet.css">
</head>
<body>
    <h1 align = 'center'><font face = 'Edwardian Script ITC'>Add Event</font></h1>
    <form method="POST" action="admin_calendar.php">
        <input value ="Back"  type="submit" style = "float: left;">
        <!-- pass 0,0,0,1, 0, currentMonth-1, year, day, month, 0 today into                   showCalendar() function-->
    </form>
    <?php
    /****************************************/
    /*************DATABASE STUFF**************/
		    $username = 'root';
			$pw = '';
			$con = mysqli_connect("localhost", $username, $pw, 'milestone');
		
			if (mysqli_connect_errno()) 
			{
				echo "Failed to connect to MySQL: " . mysqli_connect_error();
			}
		   
			 //set time to the central time zone
			if ( !date_default_timezone_set ( 'America/Chicago' ) )
			{
				echo "could not set time zone!";
			}
			
			$date = new DateTime();
			$datetime_string = date_format($date, 'Y-m-d');

    /*****************************************/
    session_start(); //allow session variables to be posted
    //echo $_SESSION['test'];
    $location = $_SESSION['location'];
    $day = $_SESSION['day'];
    $monthName = "";
    $tableDate = $_SESSION['year'] . "-" . $_SESSION['month'] . "-" . $_SESSION['day'];
    switch($_SESSION['month'])
    {
        case 1: 
			$monthName = "January";
			break;
        case 2:
			$monthName = "Febuary";
			break;
        case 3:
			$monthName = "March";
			break;
        case 4:
			$monthName = "April";
			break;
        case 5:
			$monthName = "May";
			break;
        case 6:
			$monthName = "June";
			break;
        case 7:
			$monthName = "July";
			break;
        case 8:
			$monthName = "August";
			break;
        case 9:
			$monthName = "September";
			break;
        case 10:
			$monthName = "October";
			break;
        case 11:
			$monthName = "November";
			break;
        case 12: 
			$monthName = "December";
			break;
        default:
			$monthName = "Error";
			break;
    }    
  

    $year = $_SESSION['year'];
    $date = $monthName . " " . $day . ", " . $year;
    //echo $_SESSION['location'];
    echo "<br><form method = \"POST\" action = \"\"><br>";
    echo "<br><br>Location: </br>";
    echo "<input name=\"location\" type=\"text\" value= \"$location\" size=\"20\" maxlength=\"20\"  />";
    echo "<br>Date: </br>";
    echo "<input name=\"Date: \" type=\"text\" value= \"$date\" size=\"20\" maxlength=\"20\" />";
    echo "<br>Event Name: </br>";
    echo "<input name=\"eventname\" type=\"text\" value= \" \" size=\"20\" maxlength=\"40\" />";
    echo "<br><br>Event Details: </br>";
    echo "<textarea rows=\"10\" cols=\"50\" name = 'eventInfo'>Staff Information Goes Here</textarea>";
    $querystring = "SELECT username,FirstName,LastName,isAdmin FROM users WHERE isAdmin = 0"; //select non admin users
    //select all 
    if($location == "DENTON") //write query string to get staff list for other location
    {
        $querystring2 = "SELECT staff_list FROM events WHERE date = '" . $tableDate . "'AND location = 1"; //where location is aubrey
        
    }
    else if($location == "AUBREY")
    {
        $querystring2 = "SELECT staff_list FROM events WHERE date = '" . $tableDate . "'AND location = 0"; //where location is denton
    }
     // and get contact information for display

    $result = mysqli_query($con, $querystring);//pass sql query to phpmyadmin
	
    echo "<listTitle><b><u>Select Employees</u></b></listTitle>";
    echo "<div class = box><table border = 3>";
	
	//if the query returned a result
	if ($result)
	{
		while($row = mysqli_fetch_assoc($result)) //go through all rows where condition is true and get info
		{
        
			$firstname = $row['FirstName']; //retrieve all non admin firstname, lastname, and username
			$lastname = $row['LastName'];
			$username = $row['username'];
            //loop through event at other location and see if user already assigned
            
            $result2 = mysqli_query($con, $querystring2);
            if($result2) 
            {
                $db_array = mysqli_fetch_array($result2); //fetch  information for event at other location
                $eventStaff = $db_array[0];
                $eventArray = explode(",", $eventStaff);
                $flag = 0;
                for($k=0; $k < sizeof($eventArray); $k++) //compare available staff with staff currently working at other location
                {
                    
                    if($eventArray[$k] == $username)
                    {
                        $flag = 1;
                        
                    }
                }
                if($flag == 0)
                {
			         echo "<tr><td bgcolor = white><input type = \"checkbox\" name =\"chk_group[]\" value = \"$username\"/>                              $username<br/></td></tr>";
                }
            }
            else
            {
                echo "<tr><td bgcolor = white><input type = \"checkbox\" name =\"chk_group[]\" value = \"$username\"/>                             $username<br/></td></tr>";
            }
		}		
	}
	else
	{//if query returned no result throw error message
		echo "</br>SQL query to find employees returned no results.</br>";
	}
	
    echo "</td></table></div>";
    echo "<input name = \"submitEvent\" value =\"Submit Event Details\" type=\"submit\">";
    echo "</form>";
  
    //query list of employees and display them on page for selection
    //to retrieve staff selected from user
    if (isset($_POST['chk_group'])) 
    {
        $optionArray = $_POST['chk_group'];
        for ($i=0; $i<count($optionArray); $i++) 
        {
            echo $optionArray[$i]."<br/>";
            //prints out names that were selected 
            //send these to events table
        }
        $staffString = implode(",", $optionArray); //what will be passed to events table
    }

    if(isset($_POST['submitEvent']))
    {
        if($_POST['location'] != $location) //if user changed location value then print error message
        {
            echo "<font color = 'red'>Location can only be $location in this instance!</font><br>";
        }
        else if ($_POST['location'] == $location) //if good then assign boolValue for passing to events table
        {
            if($location == "DENTON")
            {
                $boolLocation = 0; //pass 0 to events table
            }
            else if($location == "AUBREY")
            {
                $boolLocation = 1; //pass 1 to events table
            }
        }
        
        if($_POST['eventname'] == " ")
        {
           echo "<font color = 'red'>You did not enter an Event Name!</font><br>";
        }
        else
        {
            $eventName = $_POST['eventname'];//to pass to events table
        }
        
        if($_POST['eventInfo']!="")
        {
            $eventInfo = $_POST['eventInfo'];
        }
        
        //now pass all to events table
       
        $queryString = "INSERT INTO events (event_name, date, location, staff_list, event_details)                                         VALUES('".$eventName."', '".$tableDate."', '".$boolLocation."', '".$staffString."','".$eventInfo."')";
        $result = mysqli_query($con, $queryString);//returns FALSE if query fails
        
        header('location: admin_calendar.php');
    }
    

?>
</body>
</html>